package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserLoginVo;
import cn.tedu._03vehicle.pojo.vo.VehicleListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@Tag(name = "02-车辆管理模块")
@RequestMapping("/v1/vehicle")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("/add")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "添加车辆")
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");

        if(userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId((long) userLoginVo.getId());

        int i = vehicleMapper.insert(vehicle);
        if(i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.UNKNOWN_ERROR);


    }

    @GetMapping("/list")
    @ApiOperationSupport(order = 20)
    @Operation(summary = "查询车辆列表")
    @Parameters(value = {
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery", hidden = true),
    })
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, HttpSession session) {
        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");
        if(userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        List<VehicleListVo> vehicleList = vehicleMapper.listVehicle(vehicleListQuery,userLoginVo.getId());
        return JsonResult.ok(vehicleList);
    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult delVehicle(@PathVariable @Range(min = 1L, max = 500L, message = "车辆id不能小于1不能大于500b ") Integer id, HttpSession session) {
        if(id <0){
            throw new IllegalArgumentException("id不能小于0");
        }

        log.debug("删除车辆,id={}",id);

        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");

        if(userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Integer userId = userLoginVo.getId();

        Date updateTime = new Date();

        int i = vehicleMapper.delVehicle(id,userId,updateTime);
        if(i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.UNKNOWN_ERROR);
    }
}



















